package com.ybg.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * 用线程池实现需要的异步操作。
 * 
 * @author ningjh
 * @since 2016-09-12
 */
public final class AsyncUtil {
	/** 异步定时任务线程池 */
	private ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(5);

	/** 异步线程池 */
	private ExecutorService executorService = Executors.newFixedThreadPool(30);

	public <T> Future<T> submit(Callable<T> task) {
		return executorService.submit(task);
	}

	public Future<?> submit(Runnable task) {
		return executorService.submit(task);
	}

	public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
		return scheduledService.schedule(command, delay, unit);
	}

	public <T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit unit) {
		return scheduledService.schedule(callable, delay, unit);
	}

	public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
		return scheduledService.scheduleAtFixedRate(command, initialDelay, period, unit);
	}
	
	public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
		return scheduledService.scheduleWithFixedDelay(command, initialDelay, delay, unit);
	}
	
	public void close() {
		scheduledService.shutdown();
		executorService.shutdown();
		
		try {
			if (!scheduledService.awaitTermination(30, TimeUnit.SECONDS)) {
				scheduledService.shutdownNow();
			}
			
			if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
				executorService.shutdownNow();
			}
		} catch(Exception e) {
			scheduledService.shutdownNow();
			executorService.shutdownNow();
			
			LogUtil.error("关闭线程池失败", e);
		}
	}
}
